package com.l024.android_ljl.view.login;

import android.view.View;

import com.hjq.toast.ToastUtils;
import com.l024.android_ljl.MainActivity;
import com.l024.android_ljl.R;
import com.l024.android_ljl.cache.CacheManager;
import com.l024.android_ljl.constant.PageConstant;
import com.l024.android_ljl.databinding.ActivityLoginBinding;
import com.l024.android_ljl.entity.SysUserEntity;
import com.l024.android_ljl.viewmodel.login.LoginViewModel;
import com.quantstack.llcommonlibrary.base.BaseActivity;
import com.quantstack.llcommonlibrary.base.BaseViewModel;
import com.quantstack.llcommonlibrary.event.OnClickEvent;
import com.quantstack.llcommonlibrary.utils.LogUtils;

/**
 * 登录页面
 */
public class LoginActivity extends BaseActivity<LoginViewModel,ActivityLoginBinding> implements BaseViewModel.OnPageDataListener {

    private static final String TAG = "LoginActivity";

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void afterCreate() {
        //设置数据
        mViewDataBind.setUserModel(this.mViewModel);
        mViewModel.setBaseView(this);
        mViewModel.setOnPageDataListener(this);
        //跳转注册
        mViewDataBind.tvRegister.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                go(LoginActivity.this, RegisterActivity.class);
            }
        });
    }

    @Override
    public void onSuccess(String tag) {
        switch (tag){
            case PageConstant.LOGIN:{
                LogUtils.d("LoginSuccess:"+mViewModel.getSysUser().getValue().toString());
                //保存用户信息和token
                SysUserEntity userEntity = mViewModel.getSysUser().getValue();
                if(userEntity!=null){
                    CacheManager.getInstance().saveCacheUser(userEntity);
                    CacheManager.getInstance().saveCacheToken(userEntity.getToken());
                }
                go(this, MainActivity.class);
                finish();
                break;
            }
        }
    }

    @Override
    public void onError(String tag, String msg) {
        switch (tag){
            case PageConstant.LOGIN:{
                ToastUtils.show(msg);
                break;
            }
        }
    }
}